/**
 * PrimeNumbers class
 *
 * @author sergei
 *
 * @date 24 Jan 2013
 */
package prime;

import java.util.Scanner;

class PrimeNumbers {

    /**
     * Method InputBound returns number of prime numbers to count
     * that user inputs from keyboard and checks if the input is correct.
     */
    private static int inputBound() 
            throws IllegalArgumentException {
	    System.out.print("Enter boundary value of prime numbers to be found: ");
        
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        
        /* Checking if input number is positive */
	    if (n <= 0) {
            throw new IllegalArgumentException("Wrong value!");
        }

	    return n;
    }

    /**
     * Method getPrimeNumbers makes array of first n prime numbers.
     * @param PrimeArray array Object where we write found prime numbers.
     * @param int        bound Boundary value of numbers we check.
     */
    private static void getPrimeNumbers(PrimeArray array, int bound) {
        array.primeNumbersArray[0] = 2;
        array.length = 1;
        
        int current = 3;    // Number, we start search of prime nubmers from.
        while (current < bound) {
            if (array.isNotDividable(current)) {
                array.primeNumbersArray[array.length] = current;
                    // We add element to last position.
                array.length++;
            }
            current += 2;
        }
    }

    public static void main(String[] args) {
        int bound; // Number of prime numbers to be found or boundary of array

        bound = PrimeNumbers.inputBound();
        
        PrimeArray array = new PrimeArray(bound);
        
        PrimeNumbers.getPrimeNumbers(array, bound);

        array.print();
    }
}


